package com.sky.controller.user;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "店铺相关接口")
@Slf4j
@RequestMapping("/user/shop")
@RestController("userShopController")
public class ShopController {
    @Autowired
    private RedisTemplate redisTemplate;

    private final String SHOP_STATUS = "SHOP_STATUS";

    /**
     * 查询店铺营业状态
     *
     * @return
     */
    @ApiOperation("查询店铺营业状态")
    @GetMapping("/status")
    public Result getStatus() {
        try {
            // 获取Redis中的值（可能是String类型）
            Object value = redisTemplate.opsForValue().get(SHOP_STATUS);
            log.info("查询店铺营业状态：{}", value);

            // 安全地转换为Integer类型
            Integer status = null;
            if (value != null) {
                if (value instanceof String) {
                    status = Integer.valueOf((String) value);
                } else if (value instanceof Integer) {
                    status = (Integer) value;
                }
            }

            //返回结果
            return Result.success(status);
        } catch (NumberFormatException e) {
            log.error("店铺状态值格式错误", e);
            return Result.error("状态值格式错误");
        } catch (Exception e) {
            log.error("查询店铺状态失败", e);
            return Result.error("查询失败");
        }
    }
}